﻿using System;
using System.Collections.Generic;
using System.Web;
using Ue.Web;

namespace Ue.Extensions
{
    public static class HttpRequestExtensions
    {
        #region 从url或form中取值

        /// <summary>
        /// 默认RequestQueryMethod.QueryString方式获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="col"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetQueryValue<T>(this HttpRequest col, string key)
        {
            return col.GetQueryValue<T>(key, default(T), HttpRequestQueryMethod.QueryString);
        }
        public static T GetQueryValue<T>(this HttpRequest col, string key, T defaultValue)
        {
            return col.GetQueryValue<T>(key, defaultValue, HttpRequestQueryMethod.QueryString);
        }
        public static T GetQueryValue<T>(this HttpRequest col, string key, T defaultValue, HttpRequestQueryMethod requestQueryMethod)
        {
            var collection = (requestQueryMethod == HttpRequestQueryMethod.QueryString)
                ? col.QueryString
                : col.Form;

            var value = (string)collection[key];

            return value == null
                ? defaultValue
                : value.As<T>(defaultValue);
        }

        #endregion
    }
}
